/*
Continuous Testing Toolkit
Copyright (C) 2009 Paul R. Holser, Jr.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package org.infinitest.toolkit.runners;

import static org.infinitest.toolkit.internal.reflection.MethodFinder.*;
import static org.infinitest.toolkit.runners.Enclosure.*;

import java.lang.reflect.Constructor;
import java.util.List;

import org.infinitest.toolkit.internal.reflection.MethodFinder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;

class BlockJUnit4InnerClassRunner extends BlockJUnit4ClassRunner {
    private final TestClass enclosing;

    BlockJUnit4InnerClassRunner(Class<?> enclosing, Class<?> enclosed) throws InitializationError {
        super(enclosed);

        this.enclosing = new TestClass(enclosing);
    }

    @Override
    protected void collectInitializationErrors(List<Throwable> errors) {
        validatePublicVoidNoArgMethods(BeforeClass.class, true, errors);
        validatePublicVoidNoArgMethods(AfterClass.class, true, errors);
        validateInstanceMethods(errors);
    }

    @Override
    protected Enclosure createTest() throws Exception {
        Object enclosingInstance = enclosing.getOnlyConstructor().newInstance();
        Class<?> innerClass = getTestClass().getJavaClass();
        MethodFinder<?> methodFinder = methodFinderFor(innerClass);
        Constructor<?> innerCtor = methodFinder.findConstructor(new Class<?>[] { enclosing.getJavaClass() });
        innerCtor.setAccessible(true);
        Object enclosedInstance = innerCtor.newInstance(enclosingInstance);
        return new Enclosure(enclosingInstance, enclosedInstance);
    }

    @Override
    protected Statement methodInvoker(FrameworkMethod method, Object test) {
        return super.methodInvoker(method, enclosed(test));
    }

    @Override
    protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) {
        return super.possiblyExpectingExceptions(method, enclosed(test), next);
    }

    @Override
    protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) {
        return super.withPotentialTimeout(method, enclosed(test), next);
    }

    @Override
    protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
        return new RunEnclosingThenEnclosedBefores(statement, enclosing(target), enclosing, enclosed(target),
            getTestClass());
    }

    @Override
    protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
        return new RunEnclosedThenEnclosingAfters(statement, enclosed(target), getTestClass(), enclosing(target),
            enclosing);
    }
}
